package cn.iocoder.yudao.module.eval.service.subject;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.eval.controller.admin.subject.vo.*;
import cn.iocoder.yudao.module.eval.dal.dataobject.subject.EvalSubjectDO;
import cn.iocoder.yudao.module.eval.dal.mysql.subject.EvalSubjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.eval.enums.ErrorCodeConstants.*;

/**
 * 评价科目 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
@Slf4j
public class EvalSubjectServiceImpl implements EvalSubjectService {

    @Resource
    private EvalSubjectMapper subjectMapper;

    @Override
    public Long createSubject(EvalSubjectSaveReqVO createReqVO) {
        // 插入
        EvalSubjectDO subject = BeanUtils.toBean(createReqVO, EvalSubjectDO.class);
        subjectMapper.insert(subject);
        // 返回
        return subject.getId();
    }

    @Override
    public void updateSubject(EvalSubjectSaveReqVO updateReqVO) {
        // 校验存在
        validateSubjectExists(updateReqVO.getId());
        // 更新
        EvalSubjectDO updateObj = BeanUtils.toBean(updateReqVO, EvalSubjectDO.class);
        subjectMapper.updateById(updateObj);
    }

    @Override
    public void deleteSubject(Long id) {
        // 校验存在
        validateSubjectExists(id);
        // 删除
        subjectMapper.deleteById(id);
    }

    private void validateSubjectExists(Long id) {
        if (subjectMapper.selectById(id) == null) {
            throw exception(SUBJECT_NOT_EXISTS);
        }
    }

    @Override
    public EvalSubjectDO getSubject(Long id) {
        return subjectMapper.selectById(id);
    }

    @Override
    public List<EvalSubjectDO> getSubjectList(Collection<Long> ids) {
        return subjectMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<EvalSubjectDO> getSubjectPage(EvalSubjectPageReqVO pageReqVO) {
        return subjectMapper.selectPage(pageReqVO);
    }

    @Override
    public List<EvalSubjectDO> getSubjectListByPlanId(Long planId) {
        return subjectMapper.selectListByPlanId(planId);
    }

    @Override
    public List<EvalSubjectDO> getSubjectListBySubjectType(String subjectType) {
        return subjectMapper.selectListBySubjectType(subjectType);
    }

    @Override
    public List<EvalSubjectDO> getSubjectListByPlanIdAndType(Long planId, String subjectType) {
        return subjectMapper.selectListByPlanIdAndType(planId, subjectType);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<Long> batchCreateSubjects(Long planId, List<EvalSubjectSaveReqVO> subjectList) {
        List<Long> subjectIds = new ArrayList<>();
        
        for (EvalSubjectSaveReqVO subjectReqVO : subjectList) {
            subjectReqVO.setPlanId(planId);
            Long subjectId = createSubject(subjectReqVO);
            subjectIds.add(subjectId);
        }
        
        return subjectIds;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<Long> copySubjectsToOtherPlan(List<Long> sourceSubjectIds, Long targetPlanId) {
        List<Long> newSubjectIds = new ArrayList<>();
        
        for (Long sourceSubjectId : sourceSubjectIds) {
            EvalSubjectDO sourceSubject = getSubject(sourceSubjectId);
            if (sourceSubject != null) {
                // 复制科目
                EvalSubjectDO newSubject = BeanUtils.toBean(sourceSubject, EvalSubjectDO.class);
                newSubject.setId(null);
                newSubject.setPlanId(targetPlanId);
                subjectMapper.insert(newSubject);
                newSubjectIds.add(newSubject.getId());
            }
        }
        
        return newSubjectIds;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<Long> createSubjectsByTemplate(Long planId, Long templateId) {
        // TODO: 根据模板创建科目的具体实现
        // 这里需要根据模板的配置来创建对应的科目
        List<Long> subjectIds = new ArrayList<>();
        
        // 示例：创建理论知识科目
        EvalSubjectSaveReqVO theorySubject = new EvalSubjectSaveReqVO();
        theorySubject.setPlanId(planId);
        theorySubject.setSubjectName("理论知识");
        theorySubject.setSubjectType("theory");
        theorySubject.setSubjectWeight(new java.math.BigDecimal("0.4"));
        theorySubject.setPassingScore(new java.math.BigDecimal("60"));
        theorySubject.setMaxScore(new java.math.BigDecimal("100"));
        theorySubject.setExamDuration(120);
        Long theorySubjectId = createSubject(theorySubject);
        subjectIds.add(theorySubjectId);
        
        // 示例：创建技能操作科目
        EvalSubjectSaveReqVO practicalSubject = new EvalSubjectSaveReqVO();
        practicalSubject.setPlanId(planId);
        practicalSubject.setSubjectName("技能操作");
        practicalSubject.setSubjectType("practical");
        practicalSubject.setSubjectWeight(new java.math.BigDecimal("0.6"));
        practicalSubject.setPassingScore(new java.math.BigDecimal("60"));
        practicalSubject.setMaxScore(new java.math.BigDecimal("100"));
        practicalSubject.setExamDuration(180);
        Long practicalSubjectId = createSubject(practicalSubject);
        subjectIds.add(practicalSubjectId);
        
        return subjectIds;
    }

    @Override
    public List<EvalSubjectDO> getSubjectsByPlan(Long planId) {
        return subjectMapper.selectListByPlanId(planId);
    }

    @Override
    public void setSubjectSchedule(Long subjectId, EvalSubjectScheduleReqVO scheduleReqVO) {
        // TODO: 实现设置科目考试安排逻辑
    }

    @Override
    public EvalSubjectScheduleRespVO getSubjectSchedule(Long subjectId) {
        // TODO: 实现获取科目考试安排逻辑
        return new EvalSubjectScheduleRespVO();
    }

    @Override
    public void setSubjectWeight(Long subjectId, Double weight) {
        // TODO: 实现设置科目权重逻辑
    }

    @Override
    public void setSubjectPassingScore(Long subjectId, Integer passingScore) {
        // TODO: 实现设置科目及格分逻辑
    }

    @Override
    public EvalSubjectStatisticsRespVO getSubjectStatistics(Long subjectId) {
        // TODO: 实现获取科目统计信息逻辑
        return new EvalSubjectStatisticsRespVO();
    }

    @Override
    public void updateSubjectStatus(Long subjectId, Integer status) {
        // TODO: 实现更新科目状态逻辑
    }

    @Override
    public EvalPlanSubjectStatisticsRespVO getPlanSubjectStatistics(Long planId) {
        // TODO: 实现获取计划科目统计逻辑
        return new EvalPlanSubjectStatisticsRespVO();
    }

    @Override
    public EvalSubjectValidationResultRespVO validateSubjectConfig(EvalSubjectValidationReqVO validationReqVO) {
        // TODO: 实现验证科目配置逻辑
        return new EvalSubjectValidationResultRespVO();
    }

}
